package com.gs.tests.seleniumextjs;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.*;

public class RegisterForm {
	
	private WebDriver driver;
	private String validfile;
	private String invalidfile;
		
    @BeforeTest
	@Parameters({"browser","baseurl","validtestfile","invalidtestfile"})
    public void beforeTest(String browser, String url, String file1, String file2)
    {
    	driver = Utils.getDriverInstance(browser);
		driver.get(url);
		validfile = file1;
		invalidfile = file2;
		
		Utils.maximizeWindow(driver);
    }  
    
   @AfterTest
   public void afterTest() throws InterruptedException
   {	   
	   driver.quit();	   
   }
   
   @DataProvider(name = "RegisterValid")      
   public Object[][] createValidData() throws Exception {
	   Object[][] retObjArr = Utils.getTestData(validfile);
       return retObjArr;
   }
   
   @DataProvider(name = "RegisterInvalid")      
   public Object[][] createInvalidData() throws Exception {
	   Object[][] retObjArr = Utils.getTestData(invalidfile);
       return retObjArr;
   }   
   
   private void inputData(String userid, String pass, String verifyPass, String firstName,
		String lastName, String company, String email, String state, String dob) throws InterruptedException
   {
	   // identify web elements
	   WebElement _userid = driver.findElement(By.xpath("//label[contains(text(),'User ID')]/../..//input"));
	   WebElement _pass = driver.findElement(By.xpath("//label[contains(text(),'Password')]/../..//input"));
	   WebElement _verifypass = driver.findElement(By.xpath("//label[contains(text(),'Verify')]/../..//input"));
	   
	   WebElement _firstname = driver.findElement(By.xpath("//label[contains(text(),'First Name')]/../..//input"));
	   WebElement _lastname = driver.findElement(By.xpath("//label[contains(text(),'Last Name')]/../..//input"));
	   WebElement _company = driver.findElement(By.xpath("//label[contains(text(),'Company')]/../..//input"));
	   WebElement _email = driver.findElement(By.xpath("//label[contains(text(),'Email')]/../..//input"));
	   WebElement _state = driver.findElement(By.xpath("//label[contains(text(),'State')]/../..//input"));
	   WebElement _dob = driver.findElement(By.xpath("//label[contains(text(),'Date of Birth')]/../..//input"));	   
	   
	   // enter data
	   _userid.sendKeys(userid);
	   _pass.sendKeys(pass);
	   _verifypass.sendKeys(verifyPass);
	   _firstname.sendKeys(firstName);
	   _lastname.sendKeys(lastName);
	   _company.sendKeys(company);
	   _email.sendKeys(email);
	   
	   // select an item from combo-box
	   _state.sendKeys(state);
	   _state.sendKeys(Keys.RETURN);
	   
	   _dob.sendKeys(dob);	   
   }
   
   @Test(description = "Register with valid data", dataProvider = "RegisterValid")    
	public void registerValidData(String userid, String pass, String verifyPass, String firstName,
		String lastName, String company, String email, String state, String dob) throws InterruptedException
	{
	   // input data on form
	   inputData(userid, pass, verifyPass, firstName, lastName, company, email, state, dob);
	   
	   // click Register
	   WebElement register = driver.findElement(By.xpath("//a/descendant::span[contains(text(),'Register')]/ancestor::a"));
	   register.click();
	   Thread.sleep(1000);
	   
	   // verify data inputed is valid
	   List<WebElement> listErrors = driver.findElements(By.xpath("//div[@data-errorqtip!='']"));
	   Assert.assertTrue(listErrors.size()==0);
	   
	   // clear all data
	   Utils.refreshPage(driver);
	}
   
   @Test(description = "Register with invalid data", dataProvider = "RegisterInvalid")    
	public void registerInvalidData(String userid, String pass, String verifyPass, String firstName,
			String lastName, String company, String email, String state, String dob) throws InterruptedException
	{
	   // input data on form
	   inputData(userid, pass, verifyPass, firstName, lastName, company, email, state, dob);
	   Thread.sleep(2000);
	   
	   // verify data inputed is valid
	   if (userid.trim().length() == 0) {
		   WebElement _userid = driver.findElement(By.xpath("//label[contains(text(),'User ID')]/../../descendant::div[contains(@data-errorqtip,'This field is required')]"));
		   Assert.assertNotNull(_userid);
	   }
	   
	   if (pass.trim().length() == 0) {
		   WebElement _pass = driver.findElement(By.xpath("//label[contains(text(),'Password')]/../../descendant::div[contains(@data-errorqtip,'This field is required')]"));
		   Assert.assertNotNull(_pass);
	   }
	   
	   if (verifyPass.trim().length() == 0) {
		   WebElement _verifypass = driver.findElement(By.xpath("//label[contains(text(),'Verify')]/../../descendant::div[contains(@data-errorqtip,'This field is required')]"));
		   Assert.assertNotNull(_verifypass);
	   }
	   
	   WebElement _email = driver.findElement(By.xpath("//label[contains(text(),'Email')]/../../descendant::div[contains(@data-errorqtip,'This field should be an e-mail address in the format')]"));
	   Assert.assertNotNull(_email);
	   
	   WebElement _dob = driver.findElement(By.xpath("//label[contains(text(),'Date of Birth')]/../../descendant::div[contains(@data-errorqtip,'not a valid date - it must be in the format m/d/Y')]"));
	   Assert.assertNotNull(_dob);
	   
	   // clear all data
	   Utils.refreshPage(driver);
	}

}
